layui.use(['LDLayout'], function () {
    var LDLayout = layui.LDLayout,
        layer = layui.layer;

    // 设置中间内容区 最小高度
    LDLayout.setMinHeight();
    $(window).on('resize', LDLayout.setMinHeight);

    /* 图片放大镜 初始化 */
    var zoomOptions = {
        zoomType: 'standard',
        zoomWidth: 200,
        zoomHeight: 200,
        alwaysOn: false,
        title: false,
    }
    $(".zoom-hook").jqzoom(zoomOptions);

    /* 事件绑定 */

    $('.radio-item-hook').on('click', function () {
        $(this).addClass('active').siblings().removeClass('active');
    })

    $('.goods-edit')
        .delegate('.icon-reduce', 'click', function () {
            var $cartNum = $(this).siblings('.cart-num')
            var val = $cartNum.val();
            if (val - 1 >= $cartNum.data('limit')) {
                $(this).siblings('.cart-num').val(--val);
            }
        })
        .delegate('.icon-plus', 'click', function () {
            var val = $(this).siblings('.cart-num').val();
            $(this).siblings('.cart-num').val(++val);
        })
        .delegate('.cart-num', 'change', function () {
            if ($(this).val() < $(this).data('limit')) {
                $(this).val($(this).data('limit'))
            }
        })
    // 添加到购物车
    $('#add_cart_hook').on('click', function () {
        var originNum = $('.cart-action .badge').text();
        var num = $('.cart-num').val();
        $('.cart-action .badge').text(Number(originNum) + Number(num));
        LDLayout.setBadgeVisible();
    })

    // 商品图片 缩略图点击
    
    $('.thumbnail-item-wrapper a').on('click', function () {
        var $parent = $(this).closest('.thumbnail-item-wrapper')
        $parent.addClass('active').siblings().removeClass('active');
        var offsetW = $parent.outerWidth();
        var index = $parent.index();
        var imgSrc = $(this).find('img').data('src');
        var scrollOffset = (index - 1) * - offsetW;
        var limitLeft = ($('.thumbnail-item-wrapper').length - 3) * offsetW;
        if (scrollOffset > 0) {
            scrollOffset = 0;
        } else if (scrollOffset < -limitLeft) {
            scrollOffset = -limitLeft;
        }
        if ($('.thumbnail-item-wrapper a').length > 2) {
            $('.thumbnail-scroll').css('transform', 'translate(' + scrollOffset + 'px, 0)')
        }
        $('.main-picture-hook').attr('src', imgSrc);
    })

});
